//
//  NgrokConfig.cpp
//  Gngrok_mac
//
//  Created by heqian on 2019/4/7.
//  Copyright © 2019 heqian. All rights reserved.
//

#include "NgrokConfig.hpp"
#include <map>
#include <fstream>

enum yaml_type {
    none,
    value,
    node,
};

class YamlItem {
    yaml_type type;
    std::string value;
    std::map<std::string, YamlItem> node;
};

NgrokConfig::NgrokConfig() :
        serverAddress("ngrok.exgou.cn:4443"),
        userName("heqian"),
        passWord("19880727"),
        httpSubdomain("heqian"),
        httpAdd("127.0.0.1"),
        httpPort("8280"),
        rootCerts("false") {
}

int _getLeve(std::string basic_string);

void NgrokConfig::save(std::string path) {
    std::ofstream out;
    out.open(path);

    out << "server_addr: " << '"' << this->serverAddress << '"' << std::endl;
    out << "trust_host_root_certs: " << '"' << this->rootCerts << '"' << std::endl;
    out << "username: " << '"' << this->userName << '"' << std::endl;
    out << "password: " << '"' << this->passWord << '"' << std::endl;

    out << "tunnels: " << std::endl;
    out << " http: " << std::endl;
    out << "  subdomain: " << '"' << this->httpSubdomain << '"' << std::endl;
    out << "  proto: " << std::endl;
    out << "   http: " << this->httpPort << std::endl;
//    out << "   address: " << this->httpAdd << std::endl;

//    out << " https: " << std::endl;
//    out << "  subdomain: " << '"' << this->httpsSubdomain << '"' << std::endl;
//    out << "  proto: " << std::endl;
//    out << "   http: " << this->httpsPort << std::endl;
//    out << "   address: " << this->httpsAdd << std::endl;

//    out << " tcp: " << std::endl;
//    out << "  subdomain: " << '"' << this->tcpSubdomain << '"' << std::endl;
//    out << "  proto: " << std::endl;
//    out << "   http: " << this->tcpPort << std::endl;
//    out << "   address: " << this->tcpAdd << std::endl;
    out.close();
}

int _get_leve(std::string line) {
    for (int i = 0; i < line.length(); ++i) {
        if ("" != line.substr(i, i + 1)) {
            return i;
        }
    }
    return 0;
}

void _read(std::ifstream &in, std::map<std::string, YamlItem>, int depth) {
    std::string line;

    while (in >> line) {
        int d = _get_leve(line);
        if (d < depth) {

        } else if (d > depth) {

        }


    }
}

void NgrokConfig::read(std::string path) {
    std::ifstream in;
    in.open(path);

    std::map<std::string, YamlItem> root;
    _read(in, root, 0);
    in.close();
}

void NgrokConfig::setUserName(std::string name) {
    this->userName = name;
}

std::string NgrokConfig::getUserName() {
    return this->userName;
}

void NgrokConfig::setPassWord(std::string pass) {
    this->passWord = pass;
}

std::string NgrokConfig::getPassWord() {
    return this->passWord;
}


void NgrokConfig::setHttpAdd(std::string httpAdd) {
    this->httpAdd = httpAdd;
}

std::string NgrokConfig::getHttpAdd() {
    return this->httpAdd;
}


void NgrokConfig::setHttpPort(std::string httpPort) {
    this->httpPort = httpPort;
}

std::string NgrokConfig::getHttpPort() {
    return this->httpPort;
}


void NgrokConfig::setHttpsAdd(std::string httpsAdd) {
    this->httpsAdd = httpsAdd;
}

std::string NgrokConfig::getHttpsAdd() {
    return this->httpsAdd;
}


void NgrokConfig::setHttpsPort(std::string httpsPort) {
    this->httpsPort = httpsPort;
}

std::string NgrokConfig::getHttpsPort() {
    return this->httpsPort;
}

void NgrokConfig::setTcpAdd(std::string tcpAdd) {
    this->tcpAdd = tcpAdd;
}

std::string NgrokConfig::getTcpAdd() {
    return this->tcpAdd;
}


void NgrokConfig::setTcpPort(std::string tcpPort) {
    this->tcpPort = tcpPort;
}

std::string NgrokConfig::getTcpPort() {
    return this->tcpPort;
}
